package com.poetize.plus.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.poetize.plus.entity.dto.RoleDTO;
import com.poetize.plus.entity.dto.page.RolePageDTO;
import com.poetize.plus.entity.po.Role;
import com.poetize.plus.entity.po.UserRole;
import com.poetize.plus.entity.vo.page.RolePageVO;
import com.poetize.plus.entity.vo.RoleVO;
import com.poetize.plus.mapper.RoleMapper;
import com.poetize.plus.mapper.UserRoleMapper;
import com.poetize.plus.service.RoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author zuosy
 * @created 2024/3/29 11:20
 */
@Service
@RequiredArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    private final UserRoleMapper userRoleMapper;

    @Override
    public Page<RolePageVO> rolePage(RolePageDTO rolePageDTO) {
        Page<Role> reqPage = Page.of(rolePageDTO.getCurrent(), rolePageDTO.getSize());
        Page<Role> rolePage = baseMapper.selectPage(reqPage, new LambdaQueryWrapper<>());

        Page<RolePageVO> result = new Page<>();
        if (CollectionUtils.isEmpty(rolePage.getRecords())){
            result.setTotal(0);
            result.setRecords(new ArrayList<>());
            return result;
        }
        result.setTotal(rolePage.getTotal());
        result.setRecords(BeanUtil.copyToList(rolePage.getRecords(), RolePageVO.class));
        return result;
    }

    @Override
    public Boolean addRole(RoleDTO roleDTO) {
        Role checkName = lambdaQuery().eq(Role::getName, roleDTO.getName()).one();
        if (Objects.nonNull(checkName)){
            throw new IllegalArgumentException("该角色名称已存在");
        }
        Role checkCode = lambdaQuery().eq(Role::getCode, roleDTO.getCode()).one();
        if (Objects.nonNull(checkCode)){
            throw new IllegalArgumentException("该角色编码已存在");
        }
        Role role = new Role(roleDTO.getName(), roleDTO.getCode(), roleDTO.getStatus());
        return baseMapper.insert(role) > 0;
    }

    @Override
    public RoleVO queryRole(Long id) {
        Role role = baseMapper.selectById(id);
        if (Objects.isNull(role)){
            throw new IllegalArgumentException("数据异常");
        }
        return new RoleVO(role.getId(), role.getName(), role.getCode(), role.getStatus());
    }

    @Override
    public Boolean updateRole(RoleDTO roleDTO) {
        if (roleDTO.getId() == -200L){
            throw new IllegalArgumentException("禁止操作超级管理员信息");
        }
        Role checkName = lambdaQuery()
                .eq(Role::getName, roleDTO.getName())
                .ne(Role::getId, roleDTO.getId())
                .one();
        if (Objects.nonNull(checkName)){
            throw new IllegalArgumentException("该角色名称已存在");
        }
        Role checkCode = lambdaQuery()
                .eq(Role::getCode, roleDTO.getCode())
                .ne(Role::getId, roleDTO.getId())
                .one();
        if (Objects.nonNull(checkCode)){
            throw new IllegalArgumentException("该角色编码已存在");
        }
        Role role = new Role(roleDTO.getId(), roleDTO.getName(), roleDTO.getCode(), roleDTO.getStatus());
        return baseMapper.updateById(role) > 0;
    }

    @Override
    public Boolean deleteRole(Long id) {
        if (id == -200L){
            throw new IllegalArgumentException("禁止操作超级管理员信息");
        }
        List<UserRole> userRoles = userRoleMapper.selectList(new LambdaQueryWrapper<UserRole>()
                .eq(UserRole::getRoleId, id));
        if (!CollectionUtils.isEmpty(userRoles)){
            throw new IllegalArgumentException("该角色已分配账户信息,不可删除");
        }
        return baseMapper.deleteById(id) > 0;
    }
}
